﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Text.RegularExpressions;
using System.Security.Principal;
using System.Security.Cryptography;
using FractalistWX.Tool;

namespace FractalistWX
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {

        }

        protected void Session_Start(object sender, EventArgs e)
        {
           
        }
        protected void Application_AcquireRequestState(object sender, EventArgs e)
        {
            var str = Context.Request.RawUrl;

            if (str.ToLower().Contains("/manage") && (str.ToLower().Contains("controller.ashx") == false))
            {


                var c = Session["token"];



                if (c != null)
                {


                    //var userName = c.Split('|')[0];

                    //var password = c.Split('|')[1];



                    //var uid = System.Configuration.ConfigurationManager.AppSettings["userName"];
                    //var pwd = System.Configuration.ConfigurationManager.AppSettings["passWord"];


                    //if (uid == userName && pwd == password)
                    //{
                    //    return;
                    //}
                    return;
                }
                else
                {
                    Response.Redirect("/login.aspx");
                }


            }
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            var str = Context.Request.RawUrl;

            if (str.ToLower().Contains("/manage"))
            {

                //var token = userName + "|" + password;
                //var encryptToken = StrUtil.EncriptDES(token);

                //var cookie = new HttpCookie("token", Server.UrlEncode(encryptToken));
                //Response.Cookies.Add(cookie);

             //   string res = "";


               // var c = Request.Cookies["token"];

               //// c = Session["token"] as string;

               //// string res = c.Value;

               // if (c != null)
               // {
               //     var t = Server.UrlDecode(c.Value);

               //     t = StrUtil.DecriptDES(t);

               //     var userName = t.Split('|')[0];

               //     var password = t.Split('|')[1];

               //   //  res += "|" + t;

               //     //Response.Write(t);
               //     //Response.End();

               //     var uid = System.Configuration.ConfigurationManager.AppSettings["userName"];
               //     var pwd = System.Configuration.ConfigurationManager.AppSettings["passWord"];

               //     var isDebug = System.Configuration.ConfigurationManager.AppSettings["IsDebug"];

               //     //res += "&appsetting:" + uid + "|" + pwd;

               //     //if (isDebug == "true")
               //     //{

               //     //    Response.Write(res);
               //     //    Response.End();
               //     //    return;
               //     //}

               //     if (uid == userName && pwd == password)
               //     {
               //         return;
               //     }
               // }


              

               // var c = Session["token"] as string;

                // string res = c.Value;

                //if (c != null)
                //{
                   

                //    var userName = c.Split('|')[0];

                //    var password = c.Split('|')[1];

                //    //  res += "|" + t;

                //    //Response.Write(t);
                //    //Response.End();

                //    var uid = System.Configuration.ConfigurationManager.AppSettings["userName"];
                //    var pwd = System.Configuration.ConfigurationManager.AppSettings["passWord"];

                //   // var isDebug = System.Configuration.ConfigurationManager.AppSettings["IsDebug"];

                //    //res += "&appsetting:" + uid + "|" + pwd;

                //    //if (isDebug == "true")
                //    //{

                //    //    Response.Write(res);
                //    //    Response.End();
                //    //    return;
                //    //}

                //    if (uid == userName && pwd == password)
                //    {
                //        return;
                //    }
                //}

                //Response.Redirect("/login.aspx");

                //非法用户

              


            }
         //   Response.Headers["x-frame-options"] = "DENY";
            //Response.Write(str);
            //Response.End();

            //response.addHeader("x-frame-options", "DENY");
        }

        protected void  Application_EndRequest(object sender, EventArgs e)
        {
         //   Response.Write("<br/>hell3o");
           // Response.AddHeader("x-frame-options", "DENY");
        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

           

        }

        protected void Application_Error(object sender, EventArgs e)
        {
            Response.Redirect("/ErrorPage.aspx");
           

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}